package com.dk.controller;

import com.dk.pojo.CodePart;
import com.dk.pojo.CodeRule;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.CodePartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("codePart")
@Api(tags = "编码生成规则组成接口文档")
public class CodePartController {

    @Autowired
    private CodePartService codePartService;

    @GetMapping("getCodePartInfo")
    @ApiOperation("编码生成规则组成模糊查询和分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数"),
            @ApiImplicitParam(name = "partCode", value = "组成编码"),
            @ApiImplicitParam(name = "partName", value = "组成名称")
    })
    public Result getCodePartInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  String partCode, String partName){
        PageResult<CodePart> result = codePartService.getCodePartInfo(pageNum,pageSize,partCode,partName);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addCodePart")
    @ApiOperation("编码生成规则组成增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ruleId",value = "规则ID",required = true),
            @ApiImplicitParam(name = "partIndex",value = "分段序号",required = true),
            @ApiImplicitParam(name = "partType",value = "分段类型",required = true),
            @ApiImplicitParam(name = "partCode",value = "分段编号",required = false),
            @ApiImplicitParam(name = "partName",value = "分段名称",required = false),
            @ApiImplicitParam(name = "partLength",value = "分段长度",required = true),
            @ApiImplicitParam(name = "dateFormat",value = "日期格式",required = false),
            @ApiImplicitParam(name = "inputCharacter",value = "输入字符",required = false),
            @ApiImplicitParam(name = "fixCharacter",value = "固定字符",required = false),
            @ApiImplicitParam(name = "seriaStartNo",value = "流水号起始值",required = false),
            @ApiImplicitParam(name = "seriaStep",value = "流水号步长",required = false),
            @ApiImplicitParam(name = "seriaNowNo",value = "流水号当前值",required = false),
            @ApiImplicitParam(name = "cycleFlag",value = "流水号是否循环",required = false),
            @ApiImplicitParam(name = "cycleMethod",value = "循环方式",required = false),
            @ApiImplicitParam(name = "remark",value = "备注",required = false),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result addCodePart(CodePart codePart,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        return codePartService.addCodePart(codePart,token);
    }

    @PutMapping("updateCodePart")
    @ApiOperation("编码生成规则组成修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partId",value = "规则组成ID",required = true),
            @ApiImplicitParam(name = "ruleId",value = "规则ID"),
            @ApiImplicitParam(name = "partIndex",value = "分段序号"),
            @ApiImplicitParam(name = "partType",value = "分段类型"),
            @ApiImplicitParam(name = "partCode",value = "分段编号"),
            @ApiImplicitParam(name = "partName",value = "分段名称"),
            @ApiImplicitParam(name = "partLength",value = "分段长度"),
            @ApiImplicitParam(name = "dateFormat",value = "日期格式"),
            @ApiImplicitParam(name = "inputCharacter",value = "输入字符"),
            @ApiImplicitParam(name = "fixCharacter",value = "固定字符"),
            @ApiImplicitParam(name = "seriaStartNo",value = "流水号起始值"),
            @ApiImplicitParam(name = "seriaStep",value = "流水号步长"),
            @ApiImplicitParam(name = "seriaNowNo",value = "流水号当前值"),
            @ApiImplicitParam(name = "cycleFlag",value = "流水号是否循环"),
            @ApiImplicitParam(name = "cycleMethod",value = "循环方式"),
            @ApiImplicitParam(name = "remark",value = "备注"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true)
    })
    public Result updateCodePart(CodePart codePart,@RequestHeader String token){
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        return codePartService.updateCodePart(codePart,token);
    }

    @DeleteMapping("deleteCodePart")
    @ApiOperation("编码生成规则组成删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "partId", value = "编码生成规则组成id",required = true)
    })
    public Result deleteCodePart(Long partId){
        codePartService.deleteCodePart(partId);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

    @GetMapping("selectPartByRuleId")
    public List<CodePart> selectPartByRuleId(@RequestParam Long ruleId){
        return codePartService.selectPartByRuleId(ruleId);
    }

    @PutMapping("updateCodePartAPI")
    public void updateCodePartAPI(@RequestBody CodePart codePart){
        codePartService.updateCodePartAPI(codePart);
    }

}